package co.fitstart.entity.courseware.assessment;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import co.fitstart.entity.course.round.CourseRound;

/**
 * 学员评估-基础数据
 * @author haiyan_xiao
 *
 */
@Entity
@Table(name = "basic_data")
@JsonIgnoreProperties(value={"hibernate_lazy_initializer","handler","fieldHandler"})
public class BasicData {
    @Id
    @GeneratedValue
    private Long id;
    
    /** 所属课次 */
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JSONField(serialize = false)
    private CourseRound courseRound;
    
    /** 静态心率 */
    private Integer restingHeartRate;
    
    /** 最大心率 */
    private Integer maxHeartRate;
    
    /** 最佳燃脂心率 */
    private Integer burnFatRate;
    
    /** 波比跳类型：false: 简化；true:标准 */
    @Column(name = "is_standard_bobi")
    private Boolean standardBobi;
    
    /** 波比跳个数 */
    private Integer bobi;
    
    /** 心血管病史 */
    private String cardiovascular;
    
    /** 静蹲 */
    @Enumerated(EnumType.ORDINAL)
    private BasicForceOption staticSquat;
    
    /** 俯卧撑 */
    @Enumerated(EnumType.ORDINAL)
    private BasicForceOption pushUp;
    
    /** 两头起 */
    @Enumerated(EnumType.ORDINAL)
    private BasicForceOption vUp;
    
    /** 运动病史 */
    private String disease;
    
    /** 妈妈学员：腹直肌分离 */
    @Enumerated(EnumType.ORDINAL)
    private RectusAbdominisIsolation rectusAbdominisIsolation;
    
    /** 其他 */
    private String remark;
    
    private Date createTime;
    
    @Column(name = "is_disabled")
    private boolean disabled;
    
    public BasicData() {
        
    }
    
    /**
     * initialize
     * @param batch
     * @return
     */
    public BasicData initialize(CourseRound courseRound) {
        this.courseRound = courseRound;
        this.createTime = new Date();
        this.disabled = false;
        
        return this;
    }
    
    /**
     * edit
     * @param basicData
     * @return
     */
    public BasicData edit(BasicData basicData) {
        this.restingHeartRate = basicData.getRestingHeartRate();
        this.maxHeartRate = basicData.getMaxHeartRate();
        this.burnFatRate = basicData.getBurnFatRate();
        this.standardBobi = basicData.getStandardBobi();
        this.bobi = basicData.getBobi();
        this.cardiovascular = basicData.getCardiovascular();
        this.staticSquat = basicData.getStaticSquat();
        this.pushUp = basicData.getPushUp();
        this.vUp = basicData.getvUp();
        this.rectusAbdominisIsolation = basicData.getRectusAbdominisIsolation();
        this.remark = basicData.getRemark();
        
        return this;
    }
    
    /**
     * edit disease
     * @param disease
     * @return
     */
    public BasicData editDisease(List<Disease> diseases) {
        if(diseases != null && !diseases.isEmpty()) {
            StringBuffer buffer = new StringBuffer();
            
            for(Disease disease : diseases) {
                buffer.append(disease.ordinal() + ",");
            }
            
            this.disease = buffer.toString();
        }
        
        return this;
    }
    
    /**
     * 
     * @return
     */
    public List<Disease> getDiseaseList() {
        List<Disease> diseases = new ArrayList<>();
        
        if(disease != null) {
            String[] temps = disease.split(",");
            
            if(temps != null && temps.length > 0) {
                for(String temp : temps) {
                    diseases.add(Disease.ordinalOf(Integer.parseInt(temp)));
                    
                }
            }
        }
        
        return diseases;
    }

    /**
     * 
     * @return
     */
    public List<Map<String, Object>> getAllDiseases() {
        List<Map<String, Object>> result = new ArrayList<>();
        
        for(Disease disease : Disease.values()) {
            Map<String, Object> map = new HashMap<>();
            
            map.put("value", disease);
            map.put("description", disease.getDescription());
            map.put("check", this.getDisease() == null ? false : this.getDisease().contains("" + disease.ordinal()));
            
            result.add(map);
        }
        
        return result;
    }

    public boolean isShow() {
        return isShowHeartLung() || isShowBasicForce() || isShowDisease() || isShowMother();
    }

    public boolean isShowHeartLung() {
        return this.restingHeartRate != null || this.maxHeartRate != null || this.standardBobi != null || this.bobi != null || StringUtils.isNotBlank(this.cardiovascular);
    }

    public boolean isShowBasicForce() {
        return this.staticSquat != null || this.pushUp != null || this.vUp != null;
    }

    public boolean isShowDisease() {
        return this.disease != null;
    }

    public boolean isShowMother() {
        return this.rectusAbdominisIsolation != null;
    }

    public String getBobiString() {
        if(this.standardBobi != null && this.bobi != null) {
            return (this.standardBobi ? "标准" : "简化") + "," + this.bobi + "个";
        } else if(this.standardBobi != null) {
            return this.standardBobi ? "标准" : "简化";
        } else if(this.bobi != null) {
            return this.bobi + "个";
        } else {
            return null;
        }
    }

    public String getStaticSquatDesc() {
        if(this.staticSquat != null) {
            return this.staticSquat.getStaticSquat();
        } else {
            return null;
        }
    }

    public String getPushUpDesc() {
        if(this.pushUp != null) {
            return this.pushUp.getPushUp();
        } else {
            return null;
        }
    }

    public String getvUpDesc() {
        if(this.vUp != null) {
            return this.vUp.getvUp();
        } else {
            return null;
        }
    }

    public String getDiseaseString() {
        List<Disease> diseases = this.getDiseaseList();
        
        if(diseases.isEmpty()) {
            return null;
        } else if(diseases.size() == 1) {
            return diseases.get(0).getDescription();
        } else {
            StringBuffer buffer = new StringBuffer(diseases.get(0).getDescription());
            
            for(int i = 1; i<diseases.size(); i++) {
                buffer.append(",");
                buffer.append(diseases.get(i).getDescription());
            }
            
            return buffer.toString();
        }
    }

    public String getRectusAbdominisIsolationDesc() {
        if(this.rectusAbdominisIsolation != null) {
            return this.rectusAbdominisIsolation.getDescription();
        } else {
            return null;
        }
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @return the courseRound
     */
    public CourseRound getCourseRound() {
        return courseRound;
    }

    /**
     * @return the restingHeartRate
     */
    public Integer getRestingHeartRate() {
        return restingHeartRate;
    }

    /**
     * @return the maxHeartRate
     */
    public Integer getMaxHeartRate() {
        return maxHeartRate;
    }

    /**
     * @return the burnFatRate
     */
    public Integer getBurnFatRate() {
        return burnFatRate;
    }

    /**
     * @return the standardBobi
     */
    public Boolean getStandardBobi() {
        return standardBobi;
    }

    /**
     * @return the bobi
     */
    public Integer getBobi() {
        return bobi;
    }

    /**
     * @return the cardiovascular
     */
    public String getCardiovascular() {
        return cardiovascular;
    }

    /**
     * @return the staticSquat
     */
    public BasicForceOption getStaticSquat() {
        return staticSquat;
    }

    /**
     * @return the pushUp
     */
    public BasicForceOption getPushUp() {
        return pushUp;
    }

    /**
     * @return the vUp
     */
    public BasicForceOption getvUp() {
        return vUp;
    }

    /**
     * @return the disease
     */
    public String getDisease() {
        return disease;
    }

    /**
     * @return the rectusAbdominisIsolation
     */
    public RectusAbdominisIsolation getRectusAbdominisIsolation() {
        return rectusAbdominisIsolation;
    }

    /**
     * @return the remark
     */
    public String getRemark() {
        return remark;
    }

    /**
     * @return the createTime
     */
    public Date getCreateTime() {
        return createTime;
    }

    /**
     * @return the disabled
     */
    public boolean isDisabled() {
        return disabled;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @param courseRound the courseRound to set
     */
    public void setCourseRound(CourseRound courseRound) {
        this.courseRound = courseRound;
    }

    /**
     * @param restingHeartRate the restingHeartRate to set
     */
    public void setRestingHeartRate(Integer restingHeartRate) {
        this.restingHeartRate = restingHeartRate;
    }

    /**
     * @param maxHeartRate the maxHeartRate to set
     */
    public void setMaxHeartRate(Integer maxHeartRate) {
        this.maxHeartRate = maxHeartRate;
    }

    /**
     * @param burnFatRate the burnFatRate to set
     */
    public void setBurnFatRate(Integer burnFatRate) {
        this.burnFatRate = burnFatRate;
    }

    /**
     * @param standardBobi the standardBobi to set
     */
    public void setStandardBobi(Boolean standardBobi) {
        this.standardBobi = standardBobi;
    }

    /**
     * @param bobi the bobi to set
     */
    public void setBobi(Integer bobi) {
        this.bobi = bobi;
    }

    /**
     * @param cardiovascular the cardiovascular to set
     */
    public void setCardiovascular(String cardiovascular) {
        this.cardiovascular = cardiovascular;
    }

    /**
     * @param staticSquat the staticSquat to set
     */
    public void setStaticSquat(BasicForceOption staticSquat) {
        this.staticSquat = staticSquat;
    }

    /**
     * @param pushUp the pushUp to set
     */
    public void setPushUp(BasicForceOption pushUp) {
        this.pushUp = pushUp;
    }

    /**
     * @param vUp the vUp to set
     */
    public void setvUp(BasicForceOption vUp) {
        this.vUp = vUp;
    }

    /**
     * @param disease the disease to set
     */
    public void setDisease(String disease) {
        this.disease = disease;
    }

    /**
     * @param rectusAbdominisIsolation the rectusAbdominisIsolation to set
     */
    public void setRectusAbdominisIsolation(RectusAbdominisIsolation rectusAbdominisIsolation) {
        this.rectusAbdominisIsolation = rectusAbdominisIsolation;
    }

    /**
     * @param remark the remark to set
     */
    public void setRemark(String remark) {
        this.remark = remark;
    }

    /**
     * @param createTime the createTime to set
     */
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    /**
     * @param disabled the disabled to set
     */
    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }
}
